package com.nwpu.coursesupport.service.imple;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nwpu.coursesupport.criteria.ScorePerformanceSearchCriteria;
import com.nwpu.coursesupport.entity.ScorePerformance;
import com.nwpu.coursesupport.entity.Student;
import com.nwpu.coursesupport.entity.TaskAssignment;
import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.enumeration.RoleType;
import com.nwpu.coursesupport.enumeration.Status;
import com.nwpu.coursesupport.mapper.ScorePerformanceMapper;
import com.nwpu.coursesupport.mapper.StudentMapper;
import com.nwpu.coursesupport.mapper.TaskAssignmentMapper;
import com.nwpu.coursesupport.service.AbstractService;
import com.nwpu.coursesupport.service.param.IDParam;
import com.nwpu.coursesupport.service.param.ScorePerformanceServiceParam;
import com.nwpu.coursesupport.tool.UpdateUtil;
import com.nwpu.coursesupport.view.ScorePerformanceView;

@Service
public class ScorePerformanceService extends AbstractService{

	@Autowired
	private ScorePerformanceMapper scorePerformanceMapper;
	@Autowired
	private TaskAssignmentMapper taskAssignmentMapper;
	@Autowired
	private StudentMapper studentMapper;
	
    protected static Logger logger = LoggerFactory.getLogger(ScorePerformanceService.class);

	
	public JSONResult listView(IDParam p){
		JSONResult rst=new JSONResult();
		rst.fail();
		
		if(p==null||p.getId()==null){
			logger.error("参数有误");
			return rst.fail("参数有误");
		}
		
		ScorePerformanceSearchCriteria sc=new ScorePerformanceSearchCriteria();
		sc.setAssignId(p.getId());
		
		List<ScorePerformanceView> list=this.scorePerformanceMapper.findPerformanceView(sc);
		rst.addData("list", list);
		rst.success();
		return rst;
	}
	
	public JSONResult list(IDParam p){
		JSONResult rst=new JSONResult();
		rst.fail();
		
		if(p==null||p.getId()==null){
			logger.error("参数有误");
			return rst.fail("参数有误");
		}
		
		ScorePerformanceSearchCriteria sc=new ScorePerformanceSearchCriteria();
		sc.setAssignId(p.getId());
		
		List<ScorePerformance> list=this.scorePerformanceMapper.find(sc);
		rst.addData("list", list);
		rst.success();
		return rst;
	}
	
	
	public JSONResult create(ScorePerformanceServiceParam.CreateParam p){
		JSONResult rst=new JSONResult();
		rst.fail();
		
		if(p==null||p.getAssignId()==null||p.getAskStuId()==null){
			logger.error("参数错误");
			return rst.fail("参数错误");
		}
		
		TaskAssignment assign=this.taskAssignmentMapper.findById(p.getAssignId());
		if(assign==null){
			logger.error("该发布记录不存在");
			return rst.fail("该发布记录不存在");
		}
		
		Student st=this.studentMapper.findById(p.getAskStuId());
		if(st==null){
			logger.error("该学生不存在");
			return rst.fail("该学生不存在");
		}
		
		ScorePerformanceSearchCriteria sc=new ScorePerformanceSearchCriteria();
		sc.setAssignId(p.getAssignId());
		sc.setAskStuId(p.getAskStuId());
		List<ScorePerformance> list=this.scorePerformanceMapper.find(sc);
		if(list.size()>0){
			logger.error("该学生已在回答列表当中");
			return rst.fail("该学生已在回答列表当中");
		}
		ScorePerformance item=new ScorePerformance();
		UpdateUtil.setValues(item,p);
		
		if(!this.scorePerformanceMapper.save(item)){
			logger.error("添加失败");
			return rst.fail("添加失败");
		}
		
		rst.success();
		return rst;
	}
	
	public JSONResult scorePerform(ScorePerformanceServiceParam.ScorePerformParam p){
		JSONResult rst=new JSONResult();
		rst.fail();
		if(p==null||p.getId()<=0){
			logger.error("参数有误");
			return rst.fail("参数有误");
		}
		
		ScorePerformance sp=this.scorePerformanceMapper.findById(p.getId());
		sp.setMark(p.getMark());
		//sp.setDetail(p.getDetail());
		sp.setIsScore(Status.YES);
		sp.setMarkDate(new Date());
		int id=(int) getSession().getAttribute("id");
		RoleType roleType=(RoleType) getSession().getAttribute("roleType");
		sp.setMarkId(id);
		sp.setRoleType(roleType);
		if(!this.scorePerformanceMapper.update(sp)){
			logger.error("评分失败");
			return rst.fail("评分失败");
		}
		rst.success();
		return rst;
	}
}
